package com.hzt.agprosaleplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzt.agprosaleplatform.entity.Good;
import com.hzt.agprosaleplatform.mapper.GoodsMapper;
import com.hzt.agprosaleplatform.service.AdminGoodsService;
import com.hzt.agprosaleplatform.service.ShopNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    
    @Autowired
    private GoodsMapper goodsMapper;
    
    @Autowired
    private ShopNotificationService shopNotificationService;

    @Override
    public IPage<Good> getGoodsPage(Page<Good> page, String title, String shopId, String status) {
        QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
        
        // 根据条件构建查询
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }
        if (shopId != null && !shopId.isEmpty()) {
            queryWrapper.eq("shop_id", shopId);
        }
        if (status != null && !status.isEmpty()) {
            // 根据isOnline字段查询商品状态
            queryWrapper.eq("isOnline", status);
        }
        
        // 排除已删除的商品
        queryWrapper.eq("isDeleted", 0);
        
        return goodsMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Good getGoodsDetail(Integer gid) {
        return goodsMapper.selectById(gid);
    }

    @Override
    public boolean approveGoods(Integer gid, String opinion) {
        // 先查询商品信息，获取商家ID用于发送通知
        Good goods = goodsMapper.selectById(gid);
        
        // 更新商品状态为下架（0表示下架）
        com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Good> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<>();
        updateWrapper.eq("gid", gid).set("isOnline", 0); // 0表示下架
        
        int result = goodsMapper.update(null, updateWrapper);
        
        // 如果更新成功，发送通知给商家
        if (result > 0 && goods != null) {
            shopNotificationService.sendApprovalNotification(goods.getShop_id().toString(), gid, opinion);
        }
        
        return result > 0;
    }

    @Override
    public boolean rejectGoods(Integer gid, String opinion) {
        // 先查询商品信息，获取商家ID用于发送通知
        Good goods = goodsMapper.selectById(gid);
        
        // 更新商品状态为审核拒绝（2表示审核拒绝）
        com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Good> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<>();
        updateWrapper.eq("gid", gid).set("isOnline", 2); // 2表示审核拒绝
        
        int result = goodsMapper.update(null, updateWrapper);
        
        // 如果更新成功，发送拒绝通知给商家
        if (result > 0 && goods != null) {
            shopNotificationService.sendRejectionNotification(goods.getShop_id().toString(), gid, opinion);
        }
        
        return result > 0;
    }

    @Override
    public boolean offlineGoods(Integer gid, String opinion) {
        // 更新商品状态为已下架（0表示已下架）
        com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Good> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<>();
        updateWrapper.eq("gid", gid).set("isOnline", 0); // 0表示下架
        
        int result = goodsMapper.update(null, updateWrapper);
        return result > 0;
    }

    @Override
    public boolean onlineGoods(Integer gid, String opinion) {
        // 更新商品状态为已上架（1表示已上架）
        com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Good> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<>();
        updateWrapper.eq("gid", gid).set("isOnline", 1); // 1表示上架
        
        int result = goodsMapper.update(null, updateWrapper);
        return result > 0;
    }

    @Override
    public boolean deleteGoods(Integer gid, String opinion) {
        // 软删除商品，将isDeleted设置为1
        com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Good> updateWrapper = new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<>();
        updateWrapper.eq("gid", gid).set("isDeleted", 1); // 1表示已删除
        
        int result = goodsMapper.update(null, updateWrapper);
        return result > 0;
    }
}